package com.sky.service.user.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sky.constant.RedisConstant;
import com.sky.mapper.user.MenuMapper;
import com.sky.pojo.Category;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.pojo.Setmeal;
import com.sky.service.user.MenuService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
@Slf4j
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RedisTemplate redisTemplate;



    /**
     * 查询分类列表
     * @return
     */
    @Override
    public List<Category> queryList() {
        // 先从缓存中查询列表
        String jsonString = (String) redisTemplate.opsForValue().get(RedisConstant.CATEGORY_CACHE_PREFIX+"All");
        List<Category> categories;
        if (StringUtils.hasText(jsonString)) {
            categories = JSONObject.parseArray(jsonString, Category.class);
        } else {
            log.info("缓存中不存在数据,从数据库中查询");
            //缓存中不存在写入缓存
            categories = menuMapper.queryList();
            redisTemplate.opsForValue().set(RedisConstant.CATEGORY_CACHE_PREFIX+"All", JSON.toJSONString(categories));

        }
        return categories;
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<DishVO> queryDishList(Long categoryId) {
        //先从缓存中查询
        String key = RedisConstant.CATEGORY_CACHE_PREFIX+categoryId;
        String dishesJSON = (String) redisTemplate.opsForValue().get(key);
        if (StringUtils.hasText(dishesJSON)){
            //缓存中存在,转换后返回
            log.info("从缓存中查询");
            return JSONObject.parseArray(dishesJSON, DishVO.class);
        }
        log.info("从数据库中查询");
        List<DishVO> dishVOS = menuMapper.queryDishList(categoryId);
        List<Long> dishIds = dishVOS.stream().map(DishVO::getId).toList();
        List<DishFlavor> dishFlavors = menuMapper.queryByDishIds(dishIds);
        dishVOS.forEach(dishVO -> {
            dishFlavors.forEach(flavor -> {
                if (flavor.getDishId() == dishVO.getId()){
                    List<DishFlavor> flavors = dishVO.getFlavors();
                    flavors.add(flavor);
                }
            });
        });
        //写入缓存
        redisTemplate.opsForValue().set(key,JSON.toJSONString(dishVOS));
        return dishVOS;
    }

    /**
     * 根据分类id查询套餐
     * @param categoryId
     * @return
     */
    @Override
    @Cacheable(value = RedisConstant.SETMEAL_CACHE,key = "#a0")
    public List<Setmeal> querySetmealList(Integer categoryId) {
        return menuMapper.querySetmealList(categoryId);
    }


    /**
     * 根据套餐id查询包含的菜品
     */
    @Override
    public List<DishItemVO> queryDishBySId(Integer setmealId) {
        return menuMapper.queryDishBySId(setmealId);
    }
}
